from wormbane.ui.selection_dialog import selection_dialog

def command_take(game, ui):
    cell = game.hero.env
    if not cell.items:
        game.write('There is nothing here to take.')
        return
    if len(cell.items) == 1 and cell.items[0].count == 1:
        selection = {cell.items[0]: 1}
    else:
        selection = selection_dialog(ui.stdscr, list(reversed(cell.items)),
                                     prompt = 'Take')
        if not selection:
            game.write('Never mind.')
            return

    for item, count in selection.iteritems():
        if game.hero.load + item.total_weight > game.hero.capacity:
            game.write(game.hero, 'cannot carry', item)
        else:
            item = item.split(count)
            game.write(game.hero, 'takes', item)
            item.move_to(game.hero)
            item.merge()
            game.hero_moved = True
